package com.getinstaliker.screen;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.getinstaliker.instagram.InstaDefine;
import com.getinstaliker.instagram.InstagramLogin.AuthLoginListener;
import com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment;
import com.instagram.getinstaliker.R;

// TODO: Auto-generated Javadoc
/**
 * The Class LoginFragment.
 */
@SuppressLint("ValidFragment")
public class LoginFragment extends BaseGetInstalikerHeaderFragment{

	/** The m auth login listener. */
	private AuthLoginListener mAuthLoginListener;
	
	 /** The m progress dialog. */
    private ProgressDialog mProgressDialog;
    
	/**
	 * Instantiates a new login fragment.
	 *
	 * @param listener the listener
	 */
	public LoginFragment(AuthLoginListener listener) {
		mAuthLoginListener = listener;
	}
	
	/** The is showing. */
    private boolean isShowing = false;
    
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseFragmentScreen#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View vContent = inflater.inflate(R.layout.login_screen, container, false);
		initWebView(vContent);
		return vContent;
	}
	
	/**
	 * Inits the web view.
	 *
	 * @param v the v
	 */
	private void initWebView(View v){
		mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
		WebView mInstaWeb = (WebView)v.findViewById(R.id.webView);
		mInstaWeb.setVerticalScrollBarEnabled(false);
        mInstaWeb.setHorizontalScrollBarEnabled(false);
        mInstaWeb.setWebViewClient(new AuthWebViewClient());
        mInstaWeb.getSettings().setJavaScriptEnabled(true);
        mInstaWeb.getSettings().setSavePassword(false);
        mInstaWeb.requestFocus(View.FOCUS_DOWN);
        mInstaWeb.loadUrl(InstaDefine.AUTHURL);
        
        CookieSyncManager.createInstance(getActivity());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
	}

	 /**
     * Show dialog.
     * 
     * @param message the message
     */
    public void showDialog(String message) {
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }

    /**
     * Dismiss dialog.
     */
    public void dismissDialog() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
    
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#getTitleResId()
	 */
	@Override
	protected int getTitleResId() {
		return R.string.login;
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#onBack()
	 */
	@Override
	public void onBack() {
		getActivity().onBackPressed();
	}
	
	 /**
     * The Class AuthWebViewClient.
     */
    public class AuthWebViewClient extends WebViewClient {

        /*
         * (non-Javadoc)
         * 
         * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,
         * java.lang.String)
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.startsWith(InstaDefine.REDIRECT_URI)) {
                String urls[] = url.split("=");
                mAuthLoginListener.onComplete(urls[1]);
                dismissDialog();
                return true;
            }
            return false;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.webkit.WebViewClient#onPageFinished(android.webkit.WebView,
         * java.lang.String)
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dismissDialog();
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView,
         * java.lang.String, android.graphics.Bitmap)
         */
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!isShowing) {
                showDialog("Loading..");
            }

        }

        /*
         * (non-Javadoc)
         * 
         * @see android.webkit.WebViewClient#onReceivedError(android.webkit.WebView, int,
         * java.lang.String, java.lang.String)
         */
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            mAuthLoginListener.onError(description);
            dismissDialog();
        }

    }

}
